package io.knifer.gokexceltask.utils;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@SuppressWarnings("all")
public class HttpUtils {
    private HttpUtils(){}

    public static ServletContext getServletContext(){
        return getSession().getServletContext();
    }

    public static<T> T getSessionAttr(String key, Class<T> clazz){
        return (T) getSession().getAttribute(key);
    }

    public static void setSessionAttr(String key, Object value){
        getSession().setAttribute(key, value);
    }

    public static HttpSession getSession(){
        return getRequest().getSession();
    }

    public static HttpServletRequest getRequest(){
        return getServletRequestAttr().getRequest();
    }

    public static HttpServletResponse getResponse(){
        return getServletRequestAttr().getResponse();
    }

    public static ServletRequestAttributes getServletRequestAttr(){
        return ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes());
    }
}
